package com.scau.action.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.Attribute;
import com.scau.model.business.Category;
import com.scau.model.business.Rule;
import com.scau.service.impl.business.AttributeService;
import com.scau.service.impl.business.CategoryService;
import com.scau.service.impl.business.RuleService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class RuleAction extends BaseAction implements ModelDriven<Rule>, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4963006075956394620L;
	private final static Log logger = LogFactory.getLog(RuleAction.class);
	private PageController pager = null;
	private Rule rule = null;
	private RuleService ruleService = null;
	private List<Rule> ruleList = null;
	private CategoryService categoryService;
	private AttributeService attributeService;
	
	
	/**
	 * 列表显示
	 * @return
	 */
	public String list() {
		int totalRows = ruleService.getRecordCount(new Rule());
		String URL =getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		ruleList = ruleService.list(new Rule(), this.pager
				.getPageStartRow(), pager.getPageSize(),
				null,null);
		pager.setData(ruleList);
		return "list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String get() {
		rule = ruleService.get(rule);
		return "edit";
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		try {
			ruleService.save(rule);
			return "list";
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("rule", rule);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		return "list";
	}
	
	/**
	 * 改变规则的状态
	 * @return
	 */
	public String change() {
		try {
			rule = ruleService.get(rule);
			rule.setStatus(Long.parseLong(request.getParameter("status")));
			ruleService.save(rule);
			return "list";
		} catch (BusinessException e) {
			request.setAttribute("message", e.getMessage());
			return "change";
		}
	}
	/**
	 * 修订规则或初始化规则
	 * @return
	 */
	public String establish() {
		rule = ruleService.get(rule);
		return "establish";
	}
	
	/**
	 *  初始化规则细则，类别和属性
	 * @return
	 */
	public String saveCat() {
		String rule_id = request.getParameter("rule_id");
		String cat = request.getParameter("cat_p");
		if(null != cat && !"".equals(cat)){
			String[] name = cat.split(",");
			for (int i = 0; i < name.length; i++) {
				try {
					categoryService.save(new Category(rule_id,name[i]));
				} catch (BusinessException e) {
					e.printStackTrace();
				}
			}
		}
		String att = request.getParameter("att_p");
		if(null != att && !"".equals(att)){
			List<Category> catList = new CategoryService().list(new Category(), null, null, new String[]{"rule_id"},new Object[]{rule_id});
			String[] tmp = att.split("&");
			for (int i = 0; i < tmp.length; i++) {
				if(!"".equals(tmp[i])){
					String[] tmp2  = tmp[i].split("/");
					for (int j = 0; j < tmp2.length; j++) {
						String[] key_value = tmp2[j].split(",");
						try {
							if(2 == key_value.length)
								new AttributeService().save(new Attribute(catList.get(i).getId(),key_value[0],key_value[1]));
						} catch (BusinessException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		// 改变规则状态为已初始化
		Rule rule = new Rule();
		rule.setId(Long.parseLong(rule_id));
		rule = ruleService.get(rule);
		rule.setStatus(1L);
		try {
			ruleService.save(rule);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "list";
	}
	
	/**
	 * 查看规则
	 * @return
	 */
	public String check() {
		rule = ruleService.get(rule);
		List<Category> catList = categoryService.list(new Category(), null,null, new String[]{"rule_id"},new Object[]{rule.getId()});
		List<List<?>> attList = new ArrayList<List<?>>();
		for (int i = 0; i < catList.size(); i++) {
			attList.add(attributeService.list(new Attribute(), null, null, new String[]{"category_id"},new Object[]{catList.get(i).getId()}));
		}
		request.setAttribute("catList", catList);
		request.setAttribute("attList", attList);
		return "check";
	}
	

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public Rule getRule() {
		return rule;
	}

	public void setRule(Rule rule) {
		this.rule = rule;
	}
	
	public RuleService getRuleService() {
		return ruleService;
	}

	@Resource
	public void setRuleService(RuleService ruleService) {
		this.ruleService = ruleService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	@Resource
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public AttributeService getAttributeService() {
		return attributeService;
	}

	@Resource
	public void setAttributeService(AttributeService attributeService) {
		this.attributeService = attributeService;
	}

	@Override
	public Rule getModel() {
		// TODO Auto-generated method stub
		return rule;
	}
}
